package com.ruint.master.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

import com.ruint.core.app.BaseActivity;
import com.ruint.core.utils.preferences.CorePreferences;
import com.ruint.core.utils.ui.AlertUtils;
import com.ruint.core.utils.volley.RequestManager.GenericCallback;
import com.ruint.core.view.sweetalert.SweetAlertDialog;
import com.ruint.core.view.sweetalert.SweetAlertDialog.OnSweetClickListener;
import com.ruint.lib.swipeback.SwipeBackLayout;
import com.ruint.master.R;
import com.ruint.master.app.MasterApplication;
import com.ruint.master.bean.HasHeadResult;
import com.ruint.master.bean.User;
import com.ruint.master.view.Topbar;

public class LoginActivity extends BaseActivity implements OnClickListener {

  private AutoCompleteTextView mUserNameView;
  private EditText mPasswordView;
  private Button submit;
  private Topbar topbar;

  @Override
  protected int getLayoutResID() {
    return R.layout.activity_login;
  }

  @Override
  protected void pre4Create(Bundle savedInstanceState) {
    setSwipeBackEnable(SWIPE_BACK_ENABLE);
    setSwipeBackOrientation(SwipeBackLayout.EDGE_LEFT);
  }

  @Override
  protected void findViews() {
    topbar = findView(topbar, R.id.topbar);
    mUserNameView = findView(mUserNameView, R.id.phone);
    mPasswordView = findView(mPasswordView, R.id.password);
    submit = findView(submit, R.id.btn_sign_in);
  }

  @Override
  protected void handleRelevancy() {
    topbar.setTitle("登陆").setRightButton("提交").setRightListener(this);
    submit.setOnClickListener(this);
  }

  @Override
  protected void onLoadingData() {

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.btn_sign_in:
        String username = null;
        String password = null;
        username = mUserNameView.getText().toString();
        password = mPasswordView.getText().toString();
        MasterApplication.getApp().getApi()
            .login(thisInstance, username, password, new GenericCallback<HasHeadResult<User>>() {

              @Override
              public void onResponse(final HasHeadResult<User> result) {
                CorePreferences.DEBUG(result.toString());
                AlertUtils.showSimpleAlertDialog(thisInstance, "Sure?", "确定登陆吗", AlertUtils.TYPE_SUCCESS,
                    new OnSweetClickListener() {

                      @Override
                      public void onClick(SweetAlertDialog sweetAlertDialog) {
                        MasterApplication.getApp().setUser(result.getData());
                        startActivity(new Intent(thisInstance, MainActivity.class));
                        finish();
                      }
                    });
              }

              @Override
              public void onRequestFailed() {
              }

            });

        break;
    }
    /*
     * if (v == topbar.getRightButton()) {
     * MasterApplication.getApp().getApi().getDiaryList("1", "20", "0", new
     * Listener<List<Diary>>() {
     * 
     * @Override public void onResponse(List<Diary> response) {
     * ToastUtils.showActivityToast(thisInstance, Type.STANDARD, response.size()
     * + "", Animations.POPUP, null, null); } }); }
     */
  }

}
